﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
using System.Web.UI;

namespace XCommunity.Documents
{
  public class TextDocumentPlugin : IHtmlDocumentProvider
  {


    public virtual Document CreateDocument( string content, IDictionary<string, string> metaData )
    {
      DocumentMeta meta = new DocumentMeta();
      meta.Caption = metaData["Caption"];

      return new TextDocument( content, meta );
    }

    public virtual bool CanRednerDocument( Document document )
    {
      return document is TextDocument;
    }


    public virtual void RenderDocument( Document document, HtmlTextWriter writer )
    {

      if ( document == null )
        throw new ArgumentNullException( "document" );

      if ( writer == null )
        throw new ArgumentNullException( "writer" );


      TextDocument textDocument = document as TextDocument;

      if ( textDocument == null )
        throw new DocumentTypeErrorException();


      RenderDocument( textDocument, writer );
    }

    protected virtual void RenderDocument( TextDocument document, HtmlTextWriter writer )
    {
      if ( document == null )
        throw new ArgumentNullException( "document" );

      if ( writer == null )
        throw new ArgumentNullException( "writer" );


      RenderContent( document.GetContent(), writer );
    }

    protected virtual void RenderContent( string documentContent, HtmlTextWriter writer )
    {
      writer.WriteEncodedText( documentContent );
    }

  }
}
